using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.Report
{
    public partial class RP013 : ReportForm
    {
        public RP013()
        {
            InitializeComponent();
        }

        private void fld_btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RP013_Load(object sender, EventArgs e)
        {
            THSProgressBar.Start("Loading item list");
            fld_dgcICRP013ProductAttributes.Screen = this;
            fld_dgcICRP013ProductAttributes.InitializeControl();
            fld_dgcRP013ProductAttributeBranchs.Screen = this;
            fld_dgcRP013ProductAttributeBranchs.InitializeControl();

            InitializeDataSource();
            THSProgressBar.Close();
        }

        public void InitializeDataSource()
        {
            THSList<ICProductAttributesInfo> lstItems = new THSList<ICProductAttributesInfo>();
            ICProductsController objProductsController = new ICProductsController();
            DataSet ds = objProductsController.GetAllObjects();
            if (ds.Tables.Count > 0)
            {
                ICProductAttributesController objProductAttributesController = new ICProductAttributesController();
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    ICProductsInfo objProductsInfo = (ICProductsInfo)objProductsController.GetObjectFromDataRow(row);
                    DataSet ds1 = objProductAttributesController.GetLastAttributesByProductID(objProductsInfo.ICProductID);
                    if (ds1.Tables.Count > 0)
                    {
                        foreach (DataRow row1 in ds1.Tables[0].Rows)
                        {
                            ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)objProductAttributesController.GetObjectFromDataRow(row1);
                            objProductAttributesInfo.ICProductAttributeValue = objProductAttributesController.GetAttributeValueByProductAttributeID(objProductAttributesInfo.ICProductAttributeID);
                            objProductAttributesInfo.ICProductDesc = objProductsInfo.ICProductDesc;
                            objProductAttributesInfo.ICProductUnitCost = objProductsInfo.ICProductSupplierPrice;
                            objProductAttributesInfo.ICProductUnitPrice = objProductsInfo.ICProductPrice01;
                            objProductAttributesInfo.ICInventoryStockExtCost = objProductAttributesInfo.ICInventoryStockQuantity * objProductAttributesInfo.ICProductUnitCost;
                            objProductAttributesInfo.ICInventoryStockExtPrice = objProductAttributesInfo.ICInventoryStockQuantity * objProductAttributesInfo.ICProductUnitPrice;
                            lstItems.Add(objProductAttributesInfo);
                        }
                    }
                }
            }
            fld_dgcICRP013ProductAttributes.DataSource = lstItems;
            fld_dgcICRP013ProductAttributes.RefreshDataSource();
        }

        public void DisplayInventoryDetails(ICProductAttributesInfo objProductAttributesInfo)
        {
            ICProductAttributesController objProductAttributesController = new ICProductAttributesController();
            BRBranchsController objBRBranchsController = new BRBranchsController();
            THSList<ICProductAttributesInfo> lstItems = new THSList<ICProductAttributesInfo>();
            DataSet ds = objBRBranchsController.GetAllObjects();
            if (ds.Tables.Count > 0)
            {
                ICStocksController objStocksController = new ICStocksController();
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    BRBranchsInfo objBranchsInfo = (BRBranchsInfo)objBRBranchsController.GetObjectFromDataRow(row);
                    ICStocksInfo objStocksInfo = objStocksController.GetBranchStock(objBranchsInfo.BRBranchID);
                    if (objStocksInfo == null)
                        objStocksInfo = objStocksController.GetStockCentral(objBranchsInfo.BRBranchID);
                    if (objStocksInfo != null)
                    {
                        ICProductAttributesInfo objItem = (ICProductAttributesInfo)objProductAttributesInfo.Clone();
                        objItem.BRBranchName = objBranchsInfo.BRBranchName;
                        objItem.ICInventoryStockQuantity = objProductAttributesController.CalculateStockQuantity(objStocksInfo.ICStockID, objItem);
                        objItem.ICInventoryStockSaleOrderQuantity = objProductAttributesController.CalculateStockSOQuantity(objStocksInfo.ICStockID, objItem);
                        objItem.ICInventoryStockExtCost = objItem.ICInventoryStockQuantity * objItem.ICProductUnitCost;
                        objItem.ICProductUnitPrice = objItem.ICInventoryStockQuantity * objItem.ICProductUnitPrice;
                        lstItems.Add(objItem);
                    }
                }
            }
            fld_dgcRP013ProductAttributeBranchs.DataSource = lstItems;
            fld_dgcRP013ProductAttributeBranchs.RefreshDataSource();
        }
    }
}